package com.myk.game.sumeru;


import com.blankj.utilcode.util.FileUtils;
import com.myk.game.lib.databinding.ActivityPageContainerBinding;
import com.myk.game.sumeru.data.ImageDataPool;
import com.myk.game.sumeru.interfaces.IMainController;
import com.myk.game.sumeru.pages.HomePage;
import com.myk.game.lib.BgMusicHelper;
import com.myk.game.lib.base.AbsBaseActivity;

import java.io.File;

public class MainActivity extends AbsBaseActivity<ActivityPageContainerBinding> implements IMainController {

    //背景音乐开关状态
    private boolean bgMusicState;
    private ImageDataPool imageDataPool;

    @Override
    protected void init() {
        bgMusicState = true;
        openBgMusic();
        setBackground(R.drawable.bg_home);
        jumpHomePage();
    }

    @Override
    public void setBackground(int res) {
        binding.pageContainer.setBackgroundResource(res);
    }

    @Override
    public void openBgMusic() {
        bgMusicState = true;
        if (BgMusicHelper.getInstance().isMusicPlaying()) {
            BgMusicHelper.getInstance().resumeMusic();
        }
        else {
            BgMusicHelper.getInstance().startMusic(this, R.raw.bg_music, true);
        }
    }

    @Override
    public void closeBgMusic() {
        bgMusicState = false;
        BgMusicHelper.getInstance().pauseMusic();
    }

    @Override
    public boolean isMusicPlaying() {
        return bgMusicState;
    }

    @Override
    public File getApkFile() {
        /*/data/app/com.myk.game.sumeru-1/base.apk*/
        String apkPath = getPackageResourcePath();
        boolean exists = FileUtils.isFileExists(apkPath);
        File apkFile = FileUtils.getFileByPath(apkPath);
        return apkFile;
    }

    @Override
    public ImageDataPool getImageDataPool() {
        if (imageDataPool == null){
            imageDataPool = new ImageDataPool(this);
        }
        return imageDataPool;
    }

    @Override
    public void jumpHomePage() {
        binding.pageContainer.setPage(new HomePage(this));
    }

    @Override
    public void onBackPressed() {
        if (!binding.pageContainer.onBackPressed()) {
            finish();
        }
    }
}